/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.coraci.anag.gui.components.basic.gui;

import it.coraci.anag.db.mongo.entities.Attribute;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDParametrizablePanel;
import it.coraci.anag.gui.docs.ParamPanel;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Luca
 */
public class SimpleMongoParamPanel extends javax.swing.JPanel implements CRUDParametrizablePanel {

    private boolean editable = false;
    private List<Attribute> attributes;
    private List<SimpleTextParamPanel> panels = new ArrayList<SimpleTextParamPanel>();

    /**
     * Creates new form SimpleMongoParamPanel
     */
    public SimpleMongoParamPanel(List<Attribute> attributes) {
        initComponents();
        this.attributes = attributes;
        for (Attribute attribute : attributes) {
            SimpleTextParamPanel paramPanel = new SimpleTextParamPanel(attribute);
            this.panels.add(paramPanel);
            this.add(paramPanel);
        }
//        this.invalidate();
//        this.revalidate();
//        this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(102, 204, 0));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    @Override
    public void setEditable(boolean editable) {
        this.editable = editable;
        for (SimpleTextParamPanel paramPanel : panels) {
            paramPanel.setEditable(editable);
        }
    }

    @Override
    public boolean isEditable() {
        return this.editable;
    }

    @Override
    public Object getValue() {
        List<Attribute> values = new ArrayList<Attribute>();
        for (SimpleTextParamPanel simpleTextParamPanel : panels) {
            values.add(simpleTextParamPanel.getAttribute());
        }
        this.attributes.clear();
        this.attributes = values;
        return this.attributes;
    }

    @Override
    public void setValue(Object value) {
        if (value!= null && value instanceof List && (!((List)value).isEmpty())) {
            for (SimpleTextParamPanel simpleTextParamPanel : panels) {
                this.remove(simpleTextParamPanel);
            }
            this.attributes = (List<Attribute>) value;
            for (Attribute attribute : attributes) {
                SimpleTextParamPanel paramPanel = new SimpleTextParamPanel(attribute);
                paramPanel.setEditable(editable);
                this.panels.add(paramPanel);
                this.add(paramPanel);
            }
            this.invalidate();
            this.revalidate();
            this.repaint();
        }
    }

    @Override
    public void clear() {
        if (attributes != null) {
            attributes.clear();
        }
        for (SimpleTextParamPanel simpleTextParamPanel : panels) {
            this.remove(simpleTextParamPanel);
        }
        this.panels.clear();
    }
}
